package com.yjx.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.yjx.gulimall.product.entity.AttrEntity;
import com.yjx.gulimall.product.service.AttrService;
import com.yjx.gulimall.product.service.CategoryService;
import com.yjx.gulimall.common.vo.product.AttrAttrgroupRelationEntityVO;
import com.yjx.gulimall.common.vo.product.AttrGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yjx.gulimall.product.entity.AttrGroupEntity;
import com.yjx.gulimall.product.service.AttrGroupService;
import com.yjx.gulimall.common.utils.PageUtils;
import com.yjx.gulimall.common.utils.R;


/**
 * 属性分组
 *
 * @author zhuyingcoder
 * @email 1210736981@qq.com
 * @date 2022-01-23 12:30:17
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId) {
        PageUtils page = attrGroupService.queryPage(params, categoryId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {

        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        attrGroup.setCatelogPath(categoryService.findCatelogPath(catelogId));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    @RequestMapping("/{attrGroupId}/attr/relation")
    public R getAttrByAttrGroupId(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> attrEntityList = attrService.getByAttrGroupId(attrGroupId);

        return R.ok().put("data", attrEntityList);
    }


    /**
     * 获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
     *
     * @param attrGroupId
     * @return
     */
    @GetMapping("{attrgroup}/noattr/relation")
    public R getNoAttrByAttrGroupId(@PathVariable("attrgroup") Long attrGroupId, @RequestParam Map<String, Object> params) {

        PageUtils page = attrService.getNoRelationAttr(params, attrGroupId);
        return R.ok().put("page", page);
    }

    /**
     * 移除，删除 关联
     * @param relationEntityArray
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody AttrAttrgroupRelationEntityVO[] relationEntityArray){

        attrService.deleteAttRelation(relationEntityArray);
        return R.ok();
    }

    /**
     * 新增关联
     * @param relationEntityVOS
     * @return
     */
    @PostMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody AttrAttrgroupRelationEntityVO[] relationEntityVOS){

        attrService.saveAttrRelation(relationEntityVOS);

        return R.ok();
    }

    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupAttrList(@PathVariable("catelogId") Long catelogId){

        List<AttrGroupVO> attrGroupVOList = attrGroupService.getAttrGroupAttrs(catelogId);

        return R.ok().put("data",attrGroupVOList);
    }





}
